package tt.dz.service.imp.base;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tt.dz.dao.base.OmsCommonDao;
import tt.dz.entity.base.JsonCommon;
import tt.dz.entity.base.JsonForList;
import tt.dz.entity.base.OmsCommonParam;
import tt.dz.entity.base.OmsOrderThirdSet;
import tt.dz.entity.exception.OmsDaoException;
import tt.dz.service.base.OrderThirdSetService;
import tt.dz.util.OmsConstant.MsgCodeEnum;
@Service @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class OrderThirdSetServiceImpl extends BaseService implements OrderThirdSetService{
	@Autowired
	@Qualifier("base")
	private OmsCommonDao<OmsOrderThirdSet> omsCommonDaoImpl;
	
	@Override
	public synchronized  JsonCommon<?> add(OmsOrderThirdSet orderSet) 
	{
		try 
		{
			String maxId = omsCommonDaoImpl.findMaxId(OmsOrderThirdSet.class);
			maxId = genarateId(maxId,10);
			if (StringUtils.isNotEmpty(maxId))
			{
			omsCommonDaoImpl.add(orderSet);
			return this.buildSuccessJsonCommon(orderSet);
			}
		} catch (OmsDaoException e) 
		{
			this.logError(e.getMessage(), e);
			return this.buildError400JsonCommon();
		}
		return this.buildOtherJsonCommon(MsgCodeEnum.ERROR400.getMsgCode(), "Error:genarate order_no fail");
	}

	@Override
	public JsonCommon<?> update(OmsOrderThirdSet orderSet) 
	{
		if (StringUtils.isEmpty(orderSet.getOrder_no()))
		{
			return this.buildOtherJsonCommon(MsgCodeEnum.ERROR400.getMsgCode(), "Error:the request order_no is empty");
		}
		try {
			omsCommonDaoImpl.update(orderSet);
			return this.buildSuccessJsonCommon(orderSet);
		} catch (OmsDaoException e) {
			this.logError(e.getMessage(), e);
			return this.buildError400JsonCommon();
		}
	}

	@Override
	public JsonCommon<?> delete(String order_no) {
		if (StringUtils.isEmpty(order_no))
		{
			return this.buildOtherJsonCommon(MsgCodeEnum.ERROR400.getMsgCode(), "Error:the request id is empty");
		}
		try {
			omsCommonDaoImpl.deleteById(order_no, OmsOrderThirdSet.class);
			return this.buildSuccessJsonCommon("success");
		} catch (OmsDaoException e) {
			this.logError(e.getMessage(), e);
			return this.buildError400JsonCommon();
		}
	}

	@Override
	public JsonCommon<?> findById(String order_no) {
		if (StringUtils.isEmpty(order_no))
		{
			return this.buildOtherJsonCommon(MsgCodeEnum.ERROR400.getMsgCode(), "Error:the request order_no is empty");
		}
		try 
		{
			OmsOrderThirdSet orderSet = new OmsOrderThirdSet();
			orderSet.setOrder_no(order_no);
			OmsOrderThirdSet op = omsCommonDaoImpl.findRecordByPK(orderSet);
			return this.buildSuccessJsonCommon(op);
		} 
		catch (Exception e) 
		{
			this.logError(e.getMessage(), e);
			return this.buildError400JsonCommon();
		}
	}

	@Override
	public JsonForList<?> findPageObjs(OmsCommonParam param, OmsOrderThirdSet orderSet) 
	{
		try 
		{
			List<OmsOrderThirdSet> lst = omsCommonDaoImpl.pagingFind(param, orderSet);
			int total = 0;
			if (param.isQueryTotal())
			{
				total = omsCommonDaoImpl.queryTotalByObject(orderSet);
			}
			return this.buildSuccessJsonForList(param, lst, total);
		} catch (Exception e) {
			this.logError(e.getMessage(), e);
			return this.buildError400JsonForList();
		}
	}

}
